﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Core.Process.Filter;
using TjZheng.Core.Process.Handle;
using TjZheng.Core.Process.Helper;
using TjZheng.Framework;
using TjZheng.Web.Helper;
using TjZheng.WeiXin.Business;
using TjZheng.WeiXin.DataEntity;
using TjZheng.Cache;

namespace TjZheng.WeiXin.SDK.Pay
{
    public class ModeTwoServiceProcessHandle : NativePayServiceProcessHandle
    {
        /// <summary>
        /// 验证商户系统Post过来的信息
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnExecuting(ProcessExecutingContext filterContext)
        {
            ProcessHelper.CheckProcessFilterContext(filterContext);
            ModeTwoServiceProcessContext objModeTwoServiceProcessContext = filterContext.ProcessContext as ModeTwoServiceProcessContext;
            if (objModeTwoServiceProcessContext == null)
            {
                throw new ArgumentException("扫码支付模式二上下文ModeTwoServiceProcessContext为空");
            }

            InvokeResult objInvokeResult = this.CheckWeiXinPackage(objModeTwoServiceProcessContext.WeiXinPackage);
            if (objInvokeResult.ResultCode != "Success")
            {
                filterContext.Result = objInvokeResult;
                return;
            }

            objModeTwoServiceProcessContext.SetWeiXinPackage(objModeTwoServiceProcessContext.WeiXinPackage);

            this.CreateOrderInfo(objModeTwoServiceProcessContext.WeiXinPackage);
        }

        /// <summary>
        /// 生成订单二维码
        /// </summary>
        /// <param name="objProcessContext"></param>
        /// <returns></returns>
        public override InvokeResult Execute(ProcessContext objProcessContext)
        {
            ModeTwoServiceProcessContext objModeTwoServiceProcessContext = objProcessContext as ModeTwoServiceProcessContext;
            WeiXinPackage objWeiXinPackage = objModeTwoServiceProcessContext.WeiXinPackage;

            int merchantId = objWeiXinPackage.GetDictValue<int>("MerchantId");
            NativePay objNativePay = new PayModeTwo(merchantId);

            WxPayPackage objWxPayPackage = new WxPayPackage();
            objWxPayPackage.SetValue("body", this.OrderInfo.ProductDescription);//商品描述
            objWxPayPackage.SetValue("attach", this.OrderInfo.ProductAttach);//附加数据
            objWxPayPackage.SetValue("out_trade_no", this.OrderInfo.OutTradeNo);//随机字符串
            objWxPayPackage.SetValue("total_fee", this.OrderInfo.TotalFee);//总金额
            objWxPayPackage.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
            objWxPayPackage.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
            objWxPayPackage.SetValue("goods_tag", this.OrderInfo.ProductTag);//商品标记
            objWxPayPackage.SetValue("trade_type", "NATIVE");//交易类型
            objWxPayPackage.SetValue("product_id", this.OrderInfo.ProductId);//商品ID
            objWxPayPackage.SetValue("spbill_create_ip", this.OrderInfo.CreateIP);//终端ip	  	    
            objWxPayPackage.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
            objWxPayPackage.SetValue("appid", objNativePay.MerchantInfo.AppId);//公众账号ID
            objWxPayPackage.SetValue("mch_id", objNativePay.MerchantInfo.MChId);//商户号      

            //异步通知url未设置，则使用配置文件中的url
            if (!objWxPayPackage.IsValueSet("notify_url"))
            {
                objWxPayPackage.SetValue("notify_url", objNativePay.MerchantInfo.WxNotifyUrl);//异步通知url
            }
            objWxPayPackage.SetValue("sign", objWxPayPackage.CreateSign(objNativePay.MerchantInfo.MKey));//签名

            return objNativePay.CreatePayUrl(objWxPayPackage); 
        }
    }
}
